<template>
	<view class="content">
		<u-navbar title="我的标签" :placeholder='true' height="88rpx" leftIconColor="#fff"
			titleStyle='color:#fff;font-size:32rpx' left-icon-size="32rpx" bg-color="#FF594D" :autoBack="true">
		</u-navbar>
		<!-- 		<scroll-view scroll-y="true" class="tagList">
			<view></view>
		</scroll-view> -->
		<u-modal :show="tagShow" @confirm="confirmTag" title="提示" @cancel="tagId='';tagShow=false" showCancelButton
			content='是否删除标签'></u-modal>
		<view class="tagList">
			<template v-for="(item,index) in tagData">
				<view class="tag_item" @click="setTag(item)" :key="index">
					<view class="left">
						{{item.labelName}}
					</view>
					<view class="del">
						<u-button text="删除" type='error' size="mini" @click.stop="delTag(item.id)" plain></u-button>
					</view>
				</view>
			</template>
		</view>
		<view class="bottom">
			<view class="left" @click="delModel_show = true">
				添加
			</view>
		</view>
		<u-popup :show="delModel_show" round="10" mode="center">
			<view class="delModel">
				<view class="delText">
					添加标签
				</view>
				<view class="money">
					<u--input fontSize="24rpx" v-model="labelName" placeholder='请输入标签名称' :customStyle='inpCustomStyle'></u--input>
				</view>
				<u-line></u-line>
				<view class="operation">
					<view @click="delModel_show = false;labelName=''" class="btn operation_left">
						取消
					</view>
					<u-line direction='col'></u-line>
					<view @click="affirm" class="btn operation_right">
						确定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		tagList,
		addTag,
		deleteTag,
		updateMemberTag
	} from '../../../api/employee.js'
	export default {
		data() {
			return {
				titleStyle: {
					color: '#fff'
				},
				delModel_show: false,
				inpCustomStyle: {
					border: 0,
					'border-radius': '10rpx',
					'background-color': '#f3f3f3',
					'box-sizing': 'border-box',
					padding: '20rpx 30rpx',
					color: "#D4D4D4"
				},
				tagData: [],
				labelName: '',
				tagShow: false,
				tagId: '',
				ids: []
			}
		},
		created() {
			this.getTagList()
		},
		onLoad(option) {
			if (!option.ids) {
				return
			}
			this.ids = JSON.parse(option.ids)
			// console.log(option.ids);
		},
		methods: {
			setTag(tag) {
				if (this.ids[0]) {
					try {
						this.ids.forEach(async item => {
							const res = await updateMemberTag({
								id: item.id,
								// memberybDTO: {
								// 	customerInfo: {
								// 		customerUid: item.customerUid,

								// 	}
								// },
								// customerUid: item.customerUid,
								departmentId: tag.id
							})
							if (res.code != 200) {
								throw new Error(JSON.stringify(res))
							}
							console.log(res);
							// if (flag) {
							uni.showToast({
								title: '设置成功',
								success() {
									setTimeout(() => {
										uni.redirectTo({
											url: '/pages_spell/pages/member-list/member-list'
										})
									}, 1000)
								}
							})
							// }
						})
					} catch (e) {
						console.log(e);
						uni.showToast({
							title: '设置失败',
							icon: 'error'
						})
						return
					}

				}
			},
			async confirmTag() {
				const res = await deleteTag([this.tagId].join())
				console.log(res);
				if (res.code != 200) {
					uni.showToast({
						title: '删除失败',
						icon: 'error'
					})
					return
				}
				uni.showToast({
					title: '删除成功'
				})
				this.getTagList()
				this.tagId = ''
				this.tagShow = false
			},
			delTag(id) {
				this.tagShow = true
				this.tagId = id
			},
			async getTagList() {
				const res = await tagList()
				console.log(res);
				if (res.code != 200) {
					uni.showToast({
						title: '获取失败',
						icon: 'error'
					})
					return
				}
				this.tagData = res.rows
			},
			async addTags(data) {
				const res = await addTag(data)
				console.log(res);
				if (res.code != 200) {
					uni.showToast({
						title: '添加失败',
						icon: 'error'
					})
					return
				}
				uni.showToast({
					title: '添加成功'
				})
				this.getTagList()
			},
			affirm() {
				if (this.labelName != '') {
					this.addTags({
						labelName: this.labelName
					})
					this.labelName = ''
					this.delModel_show = false
					return
				}
				uni.showToast({
					title: '请输入有效名字',
					icon: 'error'
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #f3f3f3;
		min-height: 100vh;
		padding-bottom: 140rpx;
		box-sizing: border-box;

		.tagList {
			.tag_item {
				display: flex;
				height: 100rpx;
				box-sizing: border-box;
				padding: 0rpx 30rpx;
				justify-content: space-between;
				background-color: #fff;
				align-items: center;

				.left {
					font-size: 28rpx;
				}
			}
		}

		.u-safe-area-inset-bottom {
			padding-bottom: 0;
		}

		.bottom {
			width: 752rpx;
			height: 120rpx;
			background-color: #FFFFFF;
			position: fixed;
			left: 0;
			bottom: 0;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left,
			.right {
				flex: 1;
				height: 120rpx;
				background: #FF594D;
				text-align: center;
				line-height: 120rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 800;
				color: #FFFFFF;
			}
		}

		.delModel {
			width: 690rpx;
			// height: 387rpx;
			box-sizing: border-box;
			// padding: 24rpx 20rpx;

			.delTitle {
				font-size: 26rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333332;
				display: flex;
				justify-content: space-between;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}

			.delText {
				// height: 250rpx;
				text-align: center;
				padding: 40rpx;
				box-sizing: border-box;
				font-size: 26rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333332;
			}

			.money {
				padding: 0 45rpx 82rpx;
			}

			.operation {
				display: flex;
				justify-content: center;
				height: 120rpx;

				.btn {
					width: 345rpx;
					height: 100%;
					line-height: 56rpx;
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #FF594D;
					text-align: center;
					line-height: 110rpx;
					box-sizing: border-box;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
				}
			}
		}



	}
</style>